package com.wisdom.benefit.jufa.my;

import android.content.ComponentName;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.base.BaseActivity;
import com.wisdom.benefit.jufa.widget.SwitchButton;

import androidx.core.content.ContextCompat;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

public class RunPermissionSettingsActivity extends BaseActivity {
    private SwitchButton sbBtn;
    private RelativeLayout backRl;

    @Override
    protected int getLayout() {
        return R.layout.activity_run_permission_setting;
    }

    @Override
    protected void initViews() {
        //运行权限
        sbBtn = findViewById(R.id.run_permission_setting_sb);
        //返回按钮
        backRl = findViewById(R.id.account_setting_back);
        if (isEnabled()) {
            sbBtn.setChecked(true);
        } else {
            sbBtn.setChecked(false);
        }
        //设置状态栏为蓝色
        YCAppBar.setStatusBarColor(RunPermissionSettingsActivity.this,
                ContextCompat.getColor(RunPermissionSettingsActivity.this,
                        R.color.blue));
        setClick();
    }

    /**
     * 控件监听
     */
    private void setClick() {
        //返回按钮
        backRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //运行权限
        sbBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    if (!isEnabled()) {
                        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已打开", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                } else {
                    if (isEnabled()) {
                        startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已关闭", Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            }
        });
    }

    @Override
    protected int getTitleStringRes() {
        return 0;
    }

    // 判断是否打开了通知监听权限
    private boolean isEnabled() {
        String pkgName = getPackageName();
        final String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
